package com.zmykj.hik.hikService.Service.Stream.Preview;


import com.zmykj.hik.hikService.Service.RemoteManage.XMLConfig;
import com.zmykj.hik.hikService.Service.Stream.EStream;
import com.zmykj.hik.hikService.Service.CMS.ISUPCMSByJNA.NET_EHOME_PUSHSTREAM_OUT;
import com.zmykj.hik.hikService.Service.CMS.ISUPCMSByJNA.NET_EHOME_PUSHSTREAM_IN;
import com.zmykj.hik.hikService.Service.CMS.ISUPCMSByJNA.NET_EHOME_PREVIEWINFO_IN_V11;
import com.zmykj.hik.hikService.Service.CMS.ISUPCMSByJNA.NET_EHOME_PREVIEWINFO_OUT;
import com.zmykj.hik.hikService.Service.CMS.ECMS;
import com.zmykj.hik.hikService.Common.CommonMethod;

import java.util.List;

public class Preview
{

    public static long m_lPreviewSessionID = -1;

    public static boolean StartPreview(int lUserID, NET_EHOME_PREVIEWINFO_IN_V11 struPreviewIn)
    {
        CommonMethod.logRecord("INFO", "SIMPLERTMP_Init Successfully!");

        NET_EHOME_PREVIEWINFO_OUT struPreviewOut = new NET_EHOME_PREVIEWINFO_OUT();
        struPreviewOut.write();

        if(!ECMS.GetCMSInstance().NET_ECMS_StartGetRealStreamV11(lUserID, struPreviewIn, struPreviewOut.getPointer()))
        {
            CommonMethod.logRecord("ERROR", "NET_ECMS_StartGetRealStreamV11 failed, errCode is " + ECMS.GetCMSInstance().NET_ECMS_GetLastError());
            return false;
        }
        else
        {
            CommonMethod.logRecord("INFO", "NET_ECMS_StartGetRealStreamV11 Success");
            //Call NET_ECMS_XMLConfig and command GetDevAbility to obtain device capabilities and determine whether to support preview.
            //The device capability set is returned by pOutBuf.
            //If it supports, it will return to the node <NewInviteStream>, and continue with the following steps.
            String sXMLConfig =
                    "<Params>" +
                            "<ConfigCmd>GetDevAbility</ConfigCmd>" +
                            "<ConfigParam1>1</ConfigParam1>" +
                            "<ConfigParam2>0</ConfigParam2>" +
                            "<ConfigParam3>0</ConfigParam3>" +
                            "<ConfigParam4>0</ConfigParam4>" +
                            "</Params>";
            StringBuffer spConfigOutBuf = new StringBuffer();
            boolean bSupportGetStreamV4 = false;
            if(XMLConfig.CommonXMLConfig(lUserID, "GETDEVICECONFIG", sXMLConfig, spConfigOutBuf))
            {
                String sXmlOutBuf = spConfigOutBuf.toString();
                List<String> listResLabel = CommonMethod.getFieldListByRegex(sXmlOutBuf, "HRUDP");
                if(listResLabel.get(0).equals("TRUE"))
                    //support 4.0 new GetStream Protocol
                    bSupportGetStreamV4 = true;
            }
            else
            {
                CommonMethod.logRecord("ERROR", "GetDevAbility failed");
                //return false;
            }

            struPreviewOut.read();
            NET_EHOME_PUSHSTREAM_IN struPushStreamIn = new NET_EHOME_PUSHSTREAM_IN();
            struPushStreamIn.dwSize = struPushStreamIn.size();
            struPushStreamIn.lSessionID = struPreviewOut.lSessionID;

            NET_EHOME_PUSHSTREAM_OUT struPushStreamOut = new NET_EHOME_PUSHSTREAM_OUT();
            struPushStreamOut.dwSize = struPreviewOut.size();

            struPushStreamOut.dwSize = struPushStreamOut.size();
            if(!ECMS.GetCMSInstance().NET_ECMS_StartPushRealStream(lUserID, struPushStreamIn, struPushStreamOut))
            {
                CommonMethod.logRecord("ERROR", "NET_ECMS_StartPushRealStream failed, errCode:" + ECMS.GetCMSInstance().NET_ECMS_GetLastError());
                return false;
            }
            else
            {
                m_lPreviewSessionID = struPreviewOut.lSessionID.longValue();
                CommonMethod.logRecord("INFO", "NET_ECMS_StartPushRealStream Success!");
                return true;
            }
        }
    }

    public static boolean StopPreview(int lUserID){
        boolean bRet = true;
        if(!ECMS.GetCMSInstance().NET_ECMS_StopGetRealStream(lUserID, m_lPreviewSessionID))
        {
            CommonMethod.logRecord("ERROR", "NET_ECMS_StopGetRealStream failed, errCode: " + ECMS.GetCMSInstance().NET_ECMS_GetLastError());
            bRet = false;
        }
        else {
            CommonMethod.logRecord("INFO", "NET_ECMS_StopGetRealStream Success!");

            if(!EStream.GetStreamInstance().NET_ESTREAM_StopPreview(EStream.m_lNewLinkHandle))
            {
                CommonMethod.logRecord("ERROR", "NET_ESTREAM_StopPreview failed, errCode:" + EStream.GetStreamInstance().NET_ESTREAM_GetLastError());
                bRet = false;
            }
            else
            {
                CommonMethod.logRecord("INFO", "NET_ESTREAM_StopPreview Success!");
            }
        }
        return bRet;
    }
}
